//
//  main.m
//  07-HomeWork
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
//    .使用MRC, 重写用类处理的两个省市区。加入对应内存管理。
#if 0
//    1>
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/07-HomeWork/07-HomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
        if (![string hasPrefix:@" "]) {
      // 省
            Province *province = [Province zoneWithName:string];
            [allProvinceArray addObject:province];
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
     // 市
            City *city = [City zoneWithName:string];
            Province *province = [allProvinceArray lastObject];
            NSMutableArray *cityArray = province.array;
            [cityArray addObject:city];
        } else {
     // 区
            Zone *zone = [Zone zoneWithName:string];
            Province *province = [allProvinceArray lastObject];
            NSMutableArray *cityArray = province.array;
            City *city = [cityArray lastObject];
            NSMutableArray *zoneArray = city.array;
            [zoneArray addObject:zone];
        }
    }
    NSLog(@"%@", allProvinceArray);
#endif
#if 1
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/07-HomeWork/07-HomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@","];
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
        NSArray *cityArray = [string componentsSeparatedByString:@"|"];
        TempCity *tempCity = [TempCity tempCityWithName:[cityArray firstObject] number:[cityArray lastObject]];
        [allTempCityArray addObject:tempCity];
            }
//排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sort]];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.number hasSuffix:@"0000"]) {
     //省
            Province *province = [Province zoneWithTempCity:tempCity];
            [allProvinceArray addObject:province];
        } else if ([tempCity.number hasSuffix:@"00"] && ![tempCity.number hasSuffix:@"0000"]) {
    // 市
            City *city = [City zoneWithTempCity:tempCity];
            Province *province = [allProvinceArray lastObject];
            NSMutableArray *cityArray = province.array;
            [cityArray addObject:city];
        } else {
    //区
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            Province *province = [allProvinceArray lastObject];
            NSMutableArray *cityArray = province.array;
            City *city = [cityArray lastObject];
            NSMutableArray *zoneArray = city.array;
            [zoneArray addObject:zone];
        }
    }
    NSLog(@"%@", allProvinceArray);
#endif
    return 0;
}
